//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        userInfo: {},
        //nav标志
        navFlag: 0,
        loanArray: ['商业贷款', '公积金贷款', '组合贷款'],
        loanIndex: 0,
        rateArray: [{
            id: 1, name: '基准利率'
        }, {
            id: 0.7, name: '7折利率'
        }, {
            id: 0.8, name: '8折利率'
        }, {
            id: 0.83, name: '8.3折利率'
        }, {
            id: 0.85, name: '8.5折利率'
        }, {
            id: 0.88, name: '8.8折利率'
        }, {
            id: 0.9, name: '9折利率'
        }, {
            id: 0.95, name: '9.5折利率'
        }, {
            id: 1.05, name: '1.05倍利率'
        }, {
            id: 1.1, name: '1.1倍利率'
        }, {
            id: 1.2, name: '1.2倍利率'
        }, {
            id: 1.3, name: '1.3倍利率'
        }],
        rateArray1: [{
            id: 1, name: '基准利率'
        }, {
            id: 1.1, name: '1.1倍利率'
        }],
        //商贷最新基准利率
        commercial_loan: 4.75,
        //公积金贷最新基准利率
        provident_fund_loan: 2.75,
        loan: {
            //商贷利率值            
            commercial: 4.75,
            //公积金贷利率值
            providentFund: 2.75,
        },
        money: {
            //商贷金额
            money_c: "",
            //公积金贷金额
            money_p: "",
        },
        //年限
        year: 5,
        result: {
            payment: "0.00",
            decline: "0.00",
            interest: "0.00",
            total: "0.00",
        },
        detail: {
            url: "",
            text: "还款详情",
            style: ""
        }
    },
    //事件处理函数
    bindViewRate: function () {
        wx.navigateTo({
            url: '../rate/rate'
        })
    },
    onLoad: function () {
        if (app.globalData.userInfo) {
            this.setData({
                userInfo: app.globalData.userInfo
            })
        } else {
            wx.getUserInfo({
                success: res => {
                    app.globalData.userInfo = res.userInfo
                    this.setData({
                        userInfo: res.userInfo
                    })
                }
            })
        }
    },
    submit: function () {
        var that = this;
        var result = {};
        var detail = {};
        //等额本息
        if (that.data.navFlag == 0) {
            //商贷
            if (that.data.loanIndex == 0) {
                result = that.getResult(that.data.year, that.data.loan.commercial, that.data.money.money_c);
            }
            //公积金贷
            else if (that.data.loanIndex == 1) {
                result = that.getResult(that.data.year, that.data.loan.providentFund, that.data.money.money_p);
            }
            //组合贷
            else {
                var result1, result2;
                result1 = result2 = {
                    payment: 0,
                    total: 0,
                    interest: 0
                }
                if (that.data.money.money_c != "") {
                    result1 = that.getResult(that.data.year, that.data.loan.commercial, that.data.money.money_c);
                }
                if (that.data.money.money_p != "") {
                    result2 = that.getResult(that.data.year, that.data.loan.providentFund, that.data.money.money_p);
                }
                result = {
                    payment: result1.payment + result2.payment,
                    total: result1.total + result2.total,
                    interest: result1.interest + result2.interest
                }
            }

            var key1 = "result.payment";
            var key2 = "result.interest";
            var key3 = "result.total";
            that.setData({
                [key1]: result.payment.toFixed(2),
                [key2]: result.interest.toFixed(2),
                [key3]: result.total.toFixed(2),
            });
        }
        //等额本金
        else {
            //商贷、公积金贷
            if (that.data.loanIndex == 0) {
                result = that.getResult1(that.data.year, that.data.loan.commercial, that.data.money.money_c);
            }
            //公积金贷
            else if (that.data.loanIndex == 1) {
                result = that.getResult1(that.data.year, that.data.loan.providentFund, that.data.money.money_p);
            }
            //组合贷
            else {
                var result1, result2;
                result1 = result2 = {
                    payment: 0,
                    total: 0,
                    interest: 0
                }
                if (that.data.money.money_c != "") {
                    result1 = that.getResult1(that.data.year, that.data.loan.commercial, that.data.money.money_c);
                }
                if (that.data.money.money_p != "") {
                    result2 = that.getResult1(that.data.year, that.data.loan.providentFund, that.data.money.money_p);
                }
                result = {
                    payment: result1.payment + result2.payment,
                    decline: result1.decline + result2.decline,
                    total: result1.total + result2.total,
                    interest: result1.interest + result2.interest
                }
            }

            var key1 = "result.payment";
            var key2 = "result.decline";
            var key3 = "result.interest";
            var key4 = "result.total";
            that.setData({
                [key1]: result.payment.toFixed(2),
                [key2]: result.decline.toFixed(2),
                [key3]: result.interest.toFixed(2),
                [key4]: result.total.toFixed(2),
            })
        }

        //商贷
        if (that.data.loanIndex == 0) {
            detail = {
                text: "商贷还款详情",
                url: "../detail/detail?flag=" + that.data.navFlag +
                    "&year=" + that.data.year +
                    "&money_c=" + that.data.money.money_c +
                    "&commercial=" + that.data.loan.commercial,
                style: "active"
            }
        }
        //公积金贷
        else if (that.data.loanIndex == 1) {
            detail = {
                text: "公积金贷还款详情",
                url: "../detail/detail?flag=" + that.data.navFlag +
                    "&year=" + that.data.year +
                    "&money_p=" + that.data.money.money_p +
                    "&providentFund=" + that.data.loan.providentFund,
                style: "active"
            }
        }
        //组合贷
        else {
            detail = {
                text: "组合贷款还款详情",
                url: "../detail/detail?flag=" + that.data.navFlag +
                    "&year=" + that.data.year +
                    "&money_c=" + that.data.money.money_c +
                    "&commercial=" + that.data.loan.commercial +
                    "&money_p=" + that.data.money.money_p +
                    "&providentFund=" + that.data.loan.providentFund,
                style: "active"
            }
        }
        that.setData({
            detail: detail
        })
    },
    //nav切换
    navChange: function (e) {
        var that = this;
        that.setData({
            navFlag: e.target.dataset.flag,
            loanIndex: that.data.loanIndex,
            year: 5,
            commercial_loan: 4.75,
            provident_fund_loan: 2.75,
            loan: {
                commercial: 4.75,
                providentFund: 2.75,
            },
            money: {
                money_c: "",
                money_p: "",
            },
            result: {
                payment: "0.00",
                decline: "0.00",
                interest: "0.00",
                total: "0.00",
            },
            detail: {
                url: "",
                text: "还款详情",
                style: ""
            }
        })
    },
    bindLoanChange: function (e) {
        var that = this;
        var value = e.detail.value;
        if (value == 0) {    //商贷
            var key = "loan.commercial";
            that.setData({
                [key]: 4.75
            });
        } else if (value == 1) {   //公积金贷
            var key = "loan.providentFund";
            that.setData({
                [key]: 2.75
            });
        } else {                          //组合贷款
            that.setData({
                loan: {
                    commercial: 4.75,
                    providentFund: 2.75,
                },
            });
        }
        that.setData({
            loanIndex: value,
            year: 5,
            commercial_loan: 4.75,
            provident_fund_loan: 2.75,
            money: {
                money_c: "",
                money_p: "",
            },
            result: {
                payment: "0.00",
                decline: "0.00",
                interest: "0.00",
                total: "0.00",
            },
            detail: {
                url: "",
                text: "还款详情",
                style: ""
            }
        });
    },
    commercialChange: function (e) {
        var that = this;
        var rate = that.data.rateArray[e.detail.value].id * that.data.commercial_loan;
        var key = "loan.commercial";
        this.setData({
            [key]: rate.toFixed(2)
        })
    },
    providentFundChange: function (e) {
        var that = this;
        var rate = that.data.rateArray1[e.detail.value].id * that.data.provident_fund_loan;
        var key = "loan.providentFund";
        this.setData({
            [key]: rate.toFixed(2)
        })
    },
    commercialInput: function (e) {
        var that = this;
        var key = "money.money_c";
        that.setData({
            [key]: e.detail.value,
            detail: {
                url: "",
                text: "还款详情",
                style: ""
            }
        })
    },
    providentFundInput: function (e) {
        var that = this;
        var key = "money.money_p";
        that.setData({
            [key]: e.detail.value,
            detail: {
                url: "",
                text: "还款详情",
                style: ""
            }
        })
    },
    yearInput: function (e) {
        var that = this;
        var loanIndex = that.data.loanIndex,
            year = e.detail.value;
        var rate, commercial_loan, provident_fund_loan;

        if (loanIndex == 0) {
            rate = year > 5 && 4.90 || year > 1 && 4.75 || 4.35;
            var key = "loan.commercial";
            that.setData({
                year: year,
                commercial_loan: rate,
                [key]: rate,
                detail: {
                    url: "",
                    text: "还款详情",
                    style: ""
                }
            })
        } else if (loanIndex == 1) {
            rate = year > 5 && 3.25 || 2.75;
            var key = "loan.providentFund";
            that.setData({
                year: year,
                provident_fund_loan: rate,
                [key]: rate,
                detail: {
                    url: "",
                    text: "还款详情",
                    style: ""
                }
            })
        } else {
            commercial_loan = year > 5 && 4.90 || 4.75;
            provident_fund_loan = year > 5 && 3.25 || 2.75;
            var key = "loan.commercial";
            var key1 = "loan.providentFund";
            that.setData({
                year: year,
                commercial_loan: commercial_loan,
                provident_fund_loan: provident_fund_loan,
                [key]: commercial_loan,
                [key1]: provident_fund_loan,
                detail: {
                    url: "",
                    text: "还款详情",
                    style: ""
                }
            })
        }
    },
    //等额本息公式
    getResult: function (year, rate, money) {
        //还款月数
        var months = year * 12;
        //月利率
        var month_rate = rate / 100 / 12;
        //月供
        var payment = money * 10000 * month_rate * Math.pow(1 + month_rate, months) / (Math.pow(1 + month_rate, months) - 1);
        var total = payment * months;
        //总利息
        var interest = total - money * 10000;
        return {
            payment: payment,
            total: total,
            interest: interest
        }
    },
    //等额本金公式
    getResult1: function (year, rate, money) {
        //贷款本金
        var p = money * 10000;
        //还款月数
        var months = year * 12;
        //月利率
        var month_rate = rate / 100 / 12;
        //月供
        var payment = (p / months) + p * month_rate;
        //月供递减
        var decline = p / months * month_rate;
        //总利息
        var interest = ((p / months + p * month_rate) + p / months * (1 + month_rate)) / 2 * months - p;
        //还款总额
        var total = interest + p;
        return {
            payment: payment,
            decline: decline,
            total: total,
            interest: interest
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: '简易房贷计算器',
            path: '/index/index',
            imageUrl: '../../images/logo.jpg',
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    }
})
